package com.nimitz.web.controller.event;

import com.nimitz.common.core.controller.BaseController;
import com.nimitz.common.core.domain.AjaxResult;
import com.nimitz.common.core.page.TableDataInfo;
import com.nimitz.common.utils.SecurityUtils;
import com.nimitz.event.domain.Event;
import com.nimitz.event.domain.EventResult;
import com.nimitz.event.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/smart/event")
public class EventController extends BaseController{
    @Autowired
    private EventService eventService;

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('smart:event:add')")
    public AjaxResult addEvent(@RequestBody Event event){
        Long userId = SecurityUtils.getUserId();
        event.setCreateBy(userId);
        event.setUpdateBy(userId);
        eventService.addEvent(event);
        return AjaxResult.success();
    }

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('smart:event:list')")
    public TableDataInfo selectAll(@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate createTime, @RequestParam(required = false) String state){
        startPage();
        List<EventResult> list = eventService.selectAll(createTime,state);
        return getDataTable(list) ;
    }

    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('smart:event:edit')")
    public AjaxResult updateEvent(@RequestBody Event event){
        Long userId = SecurityUtils.getUserId();
        event.setUpdateBy(userId);
        eventService.updateEvent(event);
        return AjaxResult.success();
    }

    @GetMapping("/select")
    @PreAuthorize("@ss.hasPermi('smart:event:edit')")
    public AjaxResult selectById(@RequestParam Long id){
        Event event =  eventService.selectById(id);
        return AjaxResult.success(event);
    }

    @PutMapping("/delete")
    @PreAuthorize("@ss.hasPermi('smart:event:remove')")
    public AjaxResult deleteById(@RequestParam Long id){
        eventService.deleteById(id);
        return AjaxResult.success();
    }
}
